#!/bin/bash
chmod 700 .
echo "[INFO] 开始执行 KernelInvocationTiling 脚本..."
CURRENT_DIR=$(
    cd $(dirname ${BASH_SOURCE:-$0})
    pwd
)
cd $CURRENT_DIR
echo "[INFO] 当前工作目录: $CURRENT_DIR"

BUILD_TYPE="Debug"
INSTALL_PREFIX="${CURRENT_DIR}/out"

echo "[INFO] 解析命令行参数..."
SHORT=r:,v:,i:,b:,p:,
LONG=run-mode:,soc-version:,install-path:,build-type:,install-prefix:,
OPTS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
eval set -- "$OPTS"
SOC_VERSION="Ascend310P3"

while :; do
    case "$1" in
    -r | --run-mode)
        RUN_MODE="$2"
        shift 2
        ;;
    -v | --soc-version)
        SOC_VERSION="$2"
        shift 2
        ;;
    -i | --install-path)
        ASCEND_INSTALL_PATH="$2"
        shift 2
        ;;
    -b | --build-type)
        BUILD_TYPE="$2"
        shift 2
        ;;
    -p | --install-prefix)
        INSTALL_PREFIX="$2"
        shift 2
        ;;
    --)
        shift
        break
        ;;
    *)
        echo "[ERROR] Unexpected option: $1"
        break
        ;;
    esac
done

RUN_MODE_LIST="cpu sim npu"
if [[ " $RUN_MODE_LIST " != *" $RUN_MODE "* ]]; then
    echo "ERROR: RUN_MODE error, This sample only support specify cpu, sim or npu!"
    exit -1
fi
echo "[INFO] 运行模式: $RUN_MODE"

VERSION_LIST="Ascend910A Ascend910B Ascend310B1 Ascend310B2 Ascend310B3 Ascend310B4 Ascend310P1 Ascend310P3 Ascend910B1 Ascend910B2 Ascend910B3 Ascend910B4"
if [[ " $VERSION_LIST " != *" $SOC_VERSION "* ]]; then
    echo "ERROR: SOC_VERSION should be in [$VERSION_LIST]"
    exit -1
fi
echo "[INFO] SOC版本: $SOC_VERSION"

echo "[INFO] 配置Ascend环境..."
if [ -n "$ASCEND_INSTALL_PATH" ]; then
    _ASCEND_INSTALL_PATH=$ASCEND_INSTALL_PATH
    echo "[INFO] 使用指定的ASCEND_INSTALL_PATH: $_ASCEND_INSTALL_PATH"
elif [ -n "$ASCEND_HOME_PATH" ]; then
    _ASCEND_INSTALL_PATH=$ASCEND_HOME_PATH
    echo "[INFO] 使用环境变量ASCEND_HOME_PATH: $_ASCEND_INSTALL_PATH"
else
    if [ -d "$HOME/Ascend/ascend-toolkit/latest" ]; then
        _ASCEND_INSTALL_PATH=$HOME/Ascend/ascend-toolkit/latest
    else
        _ASCEND_INSTALL_PATH=/usr/local/Ascend/ascend-toolkit/latest
    fi
    echo "[INFO] 使用默认Ascend安装路径: $_ASCEND_INSTALL_PATH"
fi

export ASCEND_TOOLKIT_HOME=${_ASCEND_INSTALL_PATH}
export ASCEND_HOME_PATH=${_ASCEND_INSTALL_PATH}

echo "[INFO] 加载Ascend环境变量..."
source ${_ASCEND_INSTALL_PATH}/bin/setenv.bash

echo "[INFO] 配置运行模式环境变量..."
if [ "${RUN_MODE}" = "sim" ]; then
    echo "[INFO] 配置模拟器模式环境..."
    export LD_LIBRARY_PATH=${_ASCEND_INSTALL_PATH}/tools/simulator/${SOC_VERSION}/lib:$LD_LIBRARY_PATH
    if [ ! $CAMODEL_LOG_PATH ]; then
        export CAMODEL_LOG_PATH=$(pwd)/sim_log
    fi
    if [ -d "$CAMODEL_LOG_PATH" ]; then
        rm -rf $CAMODEL_LOG_PATH
    fi
    mkdir -p $CAMODEL_LOG_PATH
    echo "[INFO] 模拟器日志目录: $CAMODEL_LOG_PATH"
elif [ "${RUN_MODE}" = "cpu" ]; then
    echo "[INFO] 配置CPU模式环境..."
    export LD_LIBRARY_PATH=${_ASCEND_INSTALL_PATH}/tools/tikicpulib/lib:${_ASCEND_INSTALL_PATH}/tools/tikicpulib/lib/${SOC_VERSION}:${_ASCEND_INSTALL_PATH}/tools/simulator/${SOC_VERSION}/lib:$LD_LIBRARY_PATH
fi

echo "[INFO] 清理构建目录build和输出目录out"
set -e
rm -rf build out
mkdir -p build
echo "[INFO] 配置CMake参数..."
echo "[INFO]   RUN_MODE: ${RUN_MODE}"
echo "[INFO]   SOC_VERSION: ${SOC_VERSION}"
echo "[INFO]   BUILD_TYPE: ${BUILD_TYPE}"
echo "[INFO]   INSTALL_PREFIX: ${INSTALL_PREFIX}"
set -x
cmake -B build \
    -DRUN_MODE=${RUN_MODE} \
    -DSOC_VERSION=${SOC_VERSION} \
    -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
    -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \
    -DASCEND_CANN_PACKAGE_PATH=${_ASCEND_INSTALL_PATH}
echo "[INFO] 执行构建..."
cmake --build build -j
echo "[INFO] 安装构建结果..."
cmake --install build

rm -f ascendc_kernels_bbit
cp ./out/bin/ascendc_kernels_bbit ./
echo "[INFO] 已复制可执行文件: ascendc_kernels_bbit"
rm -rf input output
mkdir -p input output
echo "[INFO] 生成测试数据..."
python3 scripts/gen_data.py
echo "[INFO] 开始执行程序..."
(
    export LD_LIBRARY_PATH=$(pwd)/out/lib:$(pwd)/out/lib64:${_ASCEND_INSTALL_PATH}/lib64:$LD_LIBRARY_PATH
    RUN_WITH_TOOLCHAIN=1
    if [[ "$RUN_WITH_TOOLCHAIN" -eq 1 ]]; then
        if [ "${RUN_MODE}" = "npu" ]; then
            echo "[INFO] 在NPU模式下运行程序（带性能分析）..."
            msprof op --application=./ascendc_kernels_bbit
        elif [ "${RUN_MODE}" = "sim" ]; then
            echo "[INFO] 在模拟器模式下运行程序（带性能分析）..."
            msprof op simulator --application=./ascendc_kernels_bbit
        elif [ "${RUN_MODE}" = "cpu" ]; then
            echo "[INFO] 在CPU模式下运行程序..."
            ./ascendc_kernels_bbit
        fi
    else
        echo "[INFO] 运行程序: ./ascendc_kernels_bbit"
        ./ascendc_kernels_bbit
    fi
)
echo "[INFO] 验证执行结果..."
md5sum output/*.bin
echo "[INFO] 比较输出结果与预期结果..."
python3 scripts/verify_result.py output/output_z.bin output/golden.bin
echo "[INFO] 执行完成！"
